package tech.aistar.controller.center;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.*;
import tech.aistar.service.center.IAddressService;
import tech.aistar.service.position.IAreaService;
import tech.aistar.service.position.ICityService;
import tech.aistar.service.position.IProvinceService;
import tech.aistar.vo.AddressVo;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by 23827 on 2019/10/2.
 */
@Controller
@RequestMapping("/address")
public class CenterAddressController {

    @Autowired
    private IAddressService iAddressService;

    //展示地址
    @RequestMapping("/list")
    public String address(HttpSession session){
        Integer uid = (Integer) session.getAttribute("userId");
        List<Address> addressList = iAddressService.findAllById(uid);
        session.setAttribute("addressList",addressList);

        return "/user/centerAddress";
    }

    //删除地址
    @RequestMapping("/del")
       public String del(Model model,Integer id,HttpSession session){
        Address deladdress = iAddressService.delById(id);
        model.addAttribute("deladdress",deladdress);
        Integer uid = (Integer) session.getAttribute("userId");
        List<Address> addressList = iAddressService.findAllById(uid);
        session.setAttribute("addressList",addressList);
        return "/user/centerAddress";
    }

    //添加地址
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Address address, HttpSession session){
        System.out.println(address);
        Integer uid = (Integer) session.getAttribute("userId");
//        Address address1 = new Address();
        address.setUserId(uid);
        iAddressService.insertAddress(address);
        List<Address> addressList = iAddressService.findAllById(uid);
        session.setAttribute("addressList",addressList);
        return new Result(CodeStatus.OK,true,"添加成功!",address);
    }


    //设置默认地址
    @RequestMapping("/update")
    public String update(Model model,Integer id,HttpSession session){
        Address address1 = iAddressService.changeAll();
        model.addAttribute("address1",address1);
        Address address = iAddressService.updateById(id);
        model.addAttribute("address",address);
        Integer uid = (Integer) session.getAttribute("userId");
        List<Address> addressList = iAddressService.findAllById(uid);
        session.setAttribute("addressList",addressList);
        return "/user/centerAddress";
    }

}
